package cn.kawa.org.service.impl;

import cn.kawa.basic.query.BaseQuery;
import cn.kawa.basic.service.impl.BaseServiceImpl;
import cn.kawa.basic.util.Md5Utils;
import cn.kawa.basic.util.PageList;
import cn.kawa.basic.util.StrUtils;
import cn.kawa.org.domain.Employee;
import cn.kawa.org.mapper.EmployeeMapper;
import cn.kawa.org.query.EmployeeQuery;
import cn.kawa.org.service.IEmployeeService;
import cn.kawa.user.domain.LoginInfo;
import cn.kawa.user.mapper.LoginInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LoginInfoMapper loginInfoMapper;




    /**
     * 覆写service 的add方法，使添加Employee的同时也添加loginInfo
     * @param employee
     */
    @Override
    public void add(Employee employee) {
        /**
         * 方法重构1：保存员工时同时保存loginInfo
         */
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);//存入盐值
        String md5pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setPassword(md5pwd);//存入加密后密码
        //emp2logininfo：方法用于对象的转换 emp->loginInfo
        LoginInfo loginInfo =emp2logininfo(employee);
        loginInfoMapper.save(loginInfo);
        employee.setLogininfo_id(loginInfo.getId());
        employeeMapper.save(employee);
        /**
         * 方法重构2：如过选了角色，还要同时保存employeeRole
         * 思路：如果role_id不为空，就在employeeRole表添加一条
         * 因为employee和role是一一对应的，
         */
        if(employee.getRole_id()!=null){
            employeeMapper.addEmployeeRole(employee.getId(),employee.getRole_id());
        }

    }

    @Transactional
    @Override
    public void delete(Long id) {
        /**
         * 方法重构2：如过选了角色，还要同时删除employeeRole
         * 思路：根据员工id删除其在employeeRole的全部内容
         */
        employeeMapper.delEmployeeRole(id);//思考：为什么要放上面
        /**
         * 方法重构1：删除员工时同时删除loginInfo
         */
        loginInfoMapper.remove(employeeMapper.loadById(id).getLogininfo_id());
        employeeMapper.remove(id);


    }

    /**
     * 未完成
     * @param employee
     */
    @Transactional
    @Override
    public void update(Employee employee) {

        /**
         * 方法重构2 ：更新emp同时更新 EmployeeRole
         * 思路：先删除再添加
         */
        employeeMapper.delEmployeeRole(employee.getId());//

        if(employee.getRole_id()!=null){
            employeeMapper.addEmployeeRole(employee.getId(),employee.getRole_id());
        }

        /**
         * 方法重构1：更新emp同时更新loginInfo，先删除再添加
         */
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);//存入盐值
        String md5pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setPassword(md5pwd);
        employeeMapper.update(employee);
        //先删除对应的loginInfo
        loginInfoMapper.remove(employee.getLogininfo_id());
        //再添加loginInfo
        LoginInfo loginInfo =emp2logininfo(employee);
        loginInfoMapper.save(loginInfo);

    }

    private LoginInfo emp2logininfo(Employee admin) {
        LoginInfo loginInfo = new LoginInfo();
        BeanUtils.copyProperties(admin,loginInfo);
        loginInfo.setType(0);
        return loginInfo;
    }
    /**
     * 查询当前店铺的所有员工
     * @return
     */
    @Override
    public List<Employee> findByShopId(Long shopId) {

        return employeeMapper.findByShopId(shopId);
    }
}
